package com.cskaoyan.service.wx.impl;

import com.cskaoyan.bean.pojo.Category;
import com.cskaoyan.bean.pojo.CategoryExample;
import com.cskaoyan.bean.wx.catalog.vo.BaseCatalogDataVo;
import com.cskaoyan.bean.wx.catalog.vo.CatalogVo;
import com.cskaoyan.mapper.CategoryMapper;
import com.cskaoyan.service.wx.WxCatalogService;
import com.cskaoyan.util.ad.TransferUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @Name : WxCatalogServiceImpl.java
 * @Time : 2021/8/15 16:11
 * @Author : creamycandy
 * @Software : IntelliJ IDEA
 */

@Transactional
@Service
public class WxCatalogServiceImpl implements WxCatalogService {

    @Autowired
    CategoryMapper categoryMapper;

    @Override
    public BaseCatalogDataVo getCatalogIndex() {
        List<CatalogVo> catalogVosL1 = new ArrayList<>();
        List<CatalogVo> catalogVosL2 = new ArrayList<>();
        CategoryExample categoryExample = new CategoryExample();
        CategoryExample.Criteria criteria = categoryExample.createCriteria();
        criteria.andLevelEqualTo("L1");
        criteria.andDeletedEqualTo(false);
        List<Category> categories = categoryMapper.selectByExample(categoryExample);
        for (Category category : categories) {
            CatalogVo catalogVo = TransferUtils.Category2CatalogVo(category);
            catalogVosL1.add(catalogVo);
        }
        CatalogVo currentCatalogVo = catalogVosL1.get(0);
        CategoryExample subCategoryExample = new CategoryExample();
        CategoryExample.Criteria subCriteria = subCategoryExample.createCriteria();
//        subCriteria.andSortOrderEqualTo(currentCatalogVo.getSortOrder());
        subCriteria.andLevelEqualTo("L2");
        subCriteria.andPidEqualTo(currentCatalogVo.getId());
        subCriteria.andDeletedEqualTo(false);
        List<Category> subCategories = categoryMapper.selectByExample(subCategoryExample);
        for (Category subCategory : subCategories) {
            CatalogVo catalogVo = TransferUtils.Category2CatalogVo(subCategory);
            catalogVosL2.add(catalogVo);
        }
        return BaseCatalogDataVo.index(currentCatalogVo,catalogVosL1,catalogVosL2);
    }

    @Override
    public BaseCatalogDataVo getCatalogCurrent(Integer id) {
        List<CatalogVo> catalogVos = new ArrayList<>();
        Category category = categoryMapper.selectByPrimaryKey(id);
        CatalogVo currentCatalogVo = TransferUtils.Category2CatalogVo(category);
        CategoryExample categoryExample = new CategoryExample();
        CategoryExample.Criteria criteria = categoryExample.createCriteria();
//        criteria.andSortOrderEqualTo(category.getSortOrder());
        criteria.andLevelEqualTo("L2");
        criteria.andPidEqualTo(id);
        criteria.andDeletedEqualTo(false);
        List<Category> categories = categoryMapper.selectByExample(categoryExample);
        for (Category subcategory : categories) {
            CatalogVo catalogVo = TransferUtils.Category2CatalogVo(subcategory);
            catalogVos.add(catalogVo);
        }
        return BaseCatalogDataVo.current(currentCatalogVo,catalogVos);
    }
}
